#include <QApplication>
#include <QPainter>
#include <QStyle>

#include <messaginglayer/contactinfo.h>
#include <messaginglayer/contactsmodel.h>

#include "contactdelegate.h"

static const int m_small_picture_size = 32;
static const int m_large_picture_size = 64;

ContactDelegate::ContactDelegate(QObject *parent) :
    QStyledItemDelegate(parent),
    m_displayMode(SmallList)
{
}

ContactDelegate::DisplayMode ContactDelegate::displayMode() const
{
    return m_displayMode;
}

void ContactDelegate::setDisplayMode(DisplayMode mode)
{
    m_displayMode = mode;
}

QSize ContactDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QSize size = QStyledItemDelegate::sizeHint(option, index);
    switch (m_displayMode) {
    case LargeList:
        size.setHeight(60);
        break;
    case LargePicture:
        size.setHeight(m_large_picture_size + 40);
        size.setWidth(m_large_picture_size + 60);
        break;
    case SmallPicture:
        size.setHeight(m_small_picture_size + 40);
        size.setWidth(m_small_picture_size + 40);
        break;
    default:
        size.setHeight(25);
    }

    return size;
}

void ContactDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QStyleOptionViewItemV4 opt = option;
    initStyleOption(&opt, index);
    QString text = opt.text;
    opt.text.clear();

    const QStyleOptionViewItemV4 * optV4 = qstyleoption_cast<const QStyleOptionViewItemV4 *>(&option);
    const QWidget * w = optV4->widget;
    QStyle * style = (w) ? w->style() : QApplication::style();

    style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, w);
    //***********************************************
    QIcon picture = index.data(ContactsModel::PictureRole).value<QIcon>();
    ContactInfo::Presence presence = static_cast<ContactInfo::Presence>(index.data(ContactsModel::PresenseRole).toInt());
    int unreadMessages = index.data(ContactsModel::UnreadMessagesRole).toInt();

    if (m_displayMode == SmallPicture
        || m_displayMode == LargePicture) {
        QRect pictureRect;

        pictureRect.setTop(option.rect.top() + 10);
        if (m_displayMode == SmallPicture) {
            pictureRect.setLeft(option.rect.left() + option.rect.width() / 2 - m_small_picture_size / 2);
            pictureRect.setHeight(m_small_picture_size);
            pictureRect.setWidth(m_small_picture_size);
        } else {
            pictureRect.setLeft(option.rect.left() + option.rect.width() / 2 - m_large_picture_size / 2);
            pictureRect.setHeight(m_large_picture_size);
            pictureRect.setWidth(m_large_picture_size);
        }

        if (presence == ContactInfo::Online) {
            painter->fillRect(pictureRect.adjusted(-2, -2, 2, 2), QColor(0, 255, 0, 128));
            painter->fillRect(pictureRect, Qt::white);
            painter->drawPixmap(pictureRect, picture.pixmap(m_large_picture_size));
        } else if (presence == ContactInfo::Away) {
            painter->fillRect(pictureRect.adjusted(-2, -2, 2, 2), QColor(Qt::yellow).lighter(85));
            painter->fillRect(pictureRect, Qt::white);
            painter->drawPixmap(pictureRect, picture.pixmap(m_large_picture_size));
        } else {
            painter->drawPixmap(pictureRect, picture.pixmap(m_large_picture_size, QIcon::Disabled));
        }

        QRect textRect(option.rect.left() + 10, pictureRect.bottom() + 5, option.rect.width() - 20, option.rect.bottom() - 5);
        text = painter->fontMetrics().elidedText(text, Qt::ElideRight, textRect.width());
        painter->drawText(textRect, text, QTextOption(Qt::AlignHCenter));
    }
}

